package com.hunk.time.model;

import java.math.BigDecimal;
import java.util.Optional;

public class FruitOrder {
    //购买的水果
    private Fruit fruit;
    //购买数量 斤数
    private Integer count;
    //折扣
    private BigDecimal discount;

    public FruitOrder(Fruit fruit, Integer count) {
        this.fruit = fruit;
        this.count = count;
        this.discount = new BigDecimal(1);
    }

    public FruitOrder(Fruit fruit, Integer count, BigDecimal discount) {
        if (fruit == null) throw new RuntimeException("购买水果不能为空");
        if (count < 0) throw new RuntimeException("水果斤数为大于等于 0 的整数");
        discount = Optional.ofNullable(discount).orElse(new BigDecimal(1));
        this.fruit = fruit;
        this.count = count;
        this.discount = discount;
    }

    public Fruit getFruit() {
        return fruit;
    }

    public void setFruit(Fruit fruit) {
        this.fruit = fruit;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public BigDecimal getDiscount() {
        return discount;
    }

    public void setDiscount(BigDecimal discount) {
        this.discount = discount;
    }

    //计算订单价钱
    public BigDecimal calOrderPrice() {
        if (fruit == null) throw new RuntimeException("订单水果不能为空");
        BigDecimal price = fruit.getPrice();
        return price.multiply(new BigDecimal(count)).multiply(discount);
    }
}
